package com.hrbxlh.actionImpl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.MemberDao;
import com.hrbxlh.dao.NoticeDao;
import com.hrbxlh.dao.RelNotClaDao;
import com.hrbxlh.dao.RelStuClassDao;

@Component("noticeActionImpl")
@Transactional
public class NoticeActionImpl {
	
	@Resource(name = "noticeDao")
	private NoticeDao noticeDao;
	
	@Resource(name = "relNotClaDao")
	private RelNotClaDao relNotClaDao;
	
	@Resource(name = "relStuClassDao")
	private RelStuClassDao relStuClassDao;
	
	@Resource(name = "memberDao")
	private MemberDao memberDao;
	
	/**
	 * 查询消息设置。
	 * @throws IOException
	 */
	public Map<String, Object> getNotice(HttpSession session) throws Exception {

		Map<String, Object> returnMap = new HashMap<String, Object>();
		Map<String, Object> parameMap = new HashMap<String, Object>();
		try {
			//获取当前登陆人的工号
			String stuorteaId = (String) session.getAttribute("stuorteaId");
//			String stuorteaId = "8888888";
			parameMap.put("stuorteaId", stuorteaId);
			Map<String, Object> noticeMap = noticeDao.getNotice(parameMap);
			returnMap.put("noticeMap", noticeMap);
			
			//通过教师号获取此教师所属班级(消息通知那---结课的班级不要)
			List<String> classList = relStuClassDao.getClassByStuorteaIdAndNo(parameMap);
			returnMap.put("classList", classList);
			//通过教师号获取此教师基本信息
			List<Map<String, Object>> memberList =  memberDao.getMemberByStuorteaId(stuorteaId);
			returnMap.put("memberList", memberList);
			//通过消息主键获取此消息所有的班级
			if(noticeMap!=null){
				parameMap.put("noticeId", noticeMap.get("noticeId"));
			}
			List<String> noticeClassList = relNotClaDao.getClassByNoticeId(parameMap);
			returnMap.put("noticeClassList", noticeClassList);
		} catch (Exception e) {
			throw e;
		}
		return returnMap;
	}
	
	/**
	 * 成绩设置。
	 * @throws IOException
	 */
	public boolean addNoticeSet(Map<String,Object> paramMap,HttpSession session) throws Exception {
		
		boolean flag = false;
		boolean relFlag = false;
		boolean returnFlag = false;
		Map<String, Object> parameMap = new HashMap<String, Object>();
		try {
			//消息主键
			long noticeId = 0;
			String stuorteaId = (String) session.getAttribute("stuorteaId");
			paramMap.put("stuorteaId", stuorteaId);
			//查询消息设置是否已经有数据(不分时间)
			Map<String,Object> noticeMap = noticeDao.getNoticeBysId(paramMap);
			System.out.println("noticeMap"+noticeMap);
			if(noticeMap!=null){
				parameMap.put("noticeId", noticeMap.get("noticeId"));
				//查询消息设置关联表是否已经有数据，如果有数据则做删除操作
				List<Map<String,Object>> relResultMap = relNotClaDao.getRelNotCla(parameMap);
				System.out.println("relResultMap"+relResultMap);
				if(relResultMap!=null&&relResultMap.size()!=0){
					//有数据做删除操作
					relFlag = relNotClaDao.delRelNotCla(parameMap);
				}else{
					relFlag= true;
				}
			}else{
				relFlag= true;
			}
			//查询消息设置是否已经有数据，如果有数据则做更新操作
			Map<String,Object> resultMap = noticeDao.getNotice(paramMap);
			System.out.println("paramMap"+paramMap+"resultMap"+resultMap);
			if(resultMap!=null){
				flag = noticeDao.updateNoticeSet(paramMap);
				noticeId = (Integer) resultMap.get("noticeId");
			}else{
				//删除原有消息(有没过期的数据)
				boolean delFlag = noticeDao.delNotice(paramMap);
				if(relFlag){
					System.out.println("该走添加notice了");
					flag = noticeDao.addNoticeSet(paramMap);
					noticeId = (Long) paramMap.get("noticeId");
				}
			}
			if(flag){
				//添加消息与班级的关联表
				parameMap.put("noticeId", noticeId);
				List<Integer> classList = (List<Integer>) paramMap.get("classList");
				List<Map<String,Object>> parameList = new ArrayList<Map<String,Object>>();
				for(int i=0;i<classList.size();i++){
					Map<String,Object> classMap = new HashMap<String,Object>();
					classMap.put("noticeId", noticeId);
					classMap.put("classId", classList.get(i));
					parameList.add(classMap);
				}
				paramMap.put("parameList", parameList);
				System.out.println("最后集合"+parameList);
				relFlag = relNotClaDao.addRelNotCla(paramMap);
			}
			System.out.println("flag"+flag+"relFlag"+relFlag);
			if(flag&&relFlag){
				returnFlag = true;
			}
			
		} catch (Exception e) {
			throw e;
		}
		return returnFlag;
	}

}
